package com.datastructure.test.findelementinarray;

public class FindElementInArray {

    public int[] result = new int[2];
    public static void main(String[] args) {

    }

    public int[] findElement(int[][] mat, int n, int m, int x) {
        // write code here
        if(mat[0][0]>x||mat[n-1][m-1]<x){
            return new int[] {-1,-1};
        }
        int i=0;
        while(i<n){
            int[] ints = mat[i];
            if(findTarget(ints,0,m,i-1,x)){
                break;
            }
            i++;
        }
        return result;
    }

    public boolean findTarget(int[] mat,int start,int end,int i,int target){
        if(start>end){
            return false;
        }
        int mid = (start+end)/2;
        int value = mat[mid];
        if(value ==target){
            result[0]=i;
            result[1]=mid;
            return true;
        } else if(value<target){
           return findTarget(mat,mid+1,end,i,target);
        } else {
           return findTarget(mat,start,mid-1,i,target);
        }
    }
}
